package com.xungou.city.controller;

import com.xungou.city.pojo.City;
import com.xungou.city.service.ICityService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/city")
public class CityController {
    @Resource
    private ICityService cityService;

    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add(City city, @RequestParam Map<String, String> params) {
        int flag = cityService.insert(city,params);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag",flag);
        result.put("id",city.getId());
        result.put("code",city.getCode());
        return result;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(City city) {
        int flag = cityService.delete(city.getId());
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag",flag);
        return result;
    }

    @RequestMapping("/page")
    public ModelAndView manage(){
        ModelAndView modelAndView = new ModelAndView();
        if(SecurityUtils.getSubject().isAuthenticated()&&SecurityUtils.getSubject().hasRole("ADMIN"))
            modelAndView.setViewName("city/manage");
        else{
            SecurityUtils.getSubject().logout();
            modelAndView.setViewName("global/login");
        }
        return modelAndView;
    }

    @RequestMapping("/getCityTree")
    @ResponseBody
    public List<Map<String, Object>> getCityTree() {
        List<Map<String, Object>> result = cityService.getCityTree();
        return result;
    }

    @RequestMapping("/getCityList")
    @ResponseBody
    public List<Map<String, Object>> getCityList(@RequestParam Map<String, String> params) {
        List<Map<String, Object>> result = cityService.getCityList(params);
        return result;
    }

}